﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;
using CarlosAg.ExcelXmlWriter;

namespace BangkokSteel
{
    public partial class frmVat : Form , MainFormListener
    {
        private TabControl mainTab;
        private MySqlConnection conn = null;
        private MySqlDataAdapter adapter = null;
        private string MODE = "";

        public frmVat(TabControl mainTab)
        {
            this.mainTab = mainTab;
            InitializeComponent();
        }

        #region Form Event

        private void frmVat_Load(object sender, EventArgs e)
        {
            conn = new MySqlConnection(BangkokSteel.Properties.Settings.Default.bangkoksteelConnectionString);

            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }

            conn.Open();

            adapter = new MySqlDataAdapter("SELECT * FROM Vat", conn);
            adapter.Fill(dataSet1, "Vat");
            conn.Close();
            dataGridView1.DataSource = dataSet1.Tables["Vat"];
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.mainTab.TabPages.Remove(this.mainTab.SelectedTab);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            EnableOrDisableMainPage(false);

            MODE = "ADD";

            ClearFields();

            panel1.Visible = true;

            txtName.Focus();
            txtName.Select();
        }
            
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count > 0)
            {
                if (DialogResult.Yes == MessageBox.Show("คุณต้องการลบข้อมูลนี้ใช่หรือไม่", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    DataGridViewRow row = dataGridView1.SelectedRows[0];

                    string sql = "DELETE FROM `vat` WHERE (`id` = @Original_id)";

                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@Original_id", MySqlDbType.Int32).Value = row.Cells["id"].Value;

                    cmd.ExecuteNonQuery();

                    MessageBox.Show("ลบข้อมูลเรียบร้อยแล้ว");
                    MODE = "";

                    // Refresh for New Data
                    dataSet1.Tables["Vat"].Clear();
                    adapter.Fill(dataSet1, "Vat");
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count > 0)
            {
                EnableOrDisableMainPage(false);

                DataGridViewRow row = dataGridView1.SelectedRows[0];

                MODE = "EDIT";

                txtId.Text = row.Cells["id"].Value.ToString();
                txtName.Text = row.Cells["name"].Value.ToString();
                txtValue.Text = row.Cells["value"].Value.ToString();


                panel1.Visible = true;

                txtName.Focus();
                txtName.SelectAll();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            EnableOrDisableMainPage(true);

            panel1.Visible = false;
            MODE = "";
            ClearFields();

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text.Trim().Length == 0)
                {
                    MessageBox.Show("กรุณาเลือกใส่ชื่อให้เรียบร้อย");

                    txtName.Focus();
                    txtName.SelectAll();

                    return;
                }

                MySqlCommandBuilder builder = new MySqlCommandBuilder(adapter);

                if ("ADD".Equals(MODE))
                {
                    MySqlCommand cmd = (MySqlCommand)builder.GetInsertCommand(true);
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@name", MySqlDbType.VarChar).Value = txtName.Text;
                    cmd.Parameters.Add("@value", MySqlDbType.Double).Value = txtValue.Text;

                    int affectedRow = cmd.ExecuteNonQuery();

                    MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว");
                    ClearFields();

                    txtName.Focus();
                    txtName.SelectAll();

                }
                else if ("EDIT".Equals(MODE))
                {
                    string sql = @"UPDATE `vat` SET `name` = @name, `value` = @value WHERE (`id` = @Original_id)";

                    MySqlCommand cmd = new MySqlCommand(sql, conn);
                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@name", MySqlDbType.VarChar).Value = txtName.Text;
                    cmd.Parameters.Add("@value", MySqlDbType.Double).Value = txtValue.Text;
                    cmd.Parameters.Add("@Original_id", MySqlDbType.Int32).Value = txtId.Text;

                    cmd.ExecuteNonQuery();

                    MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว");

                    panel1.Visible = false;
                    ClearFields();

                    MODE = "";
                }
                // Refresh for New Data
                dataSet1.Tables["Vat"].Clear();
                adapter.Fill(dataSet1, "Vat");

                EnableOrDisableMainPage(true);

            }
            catch (FormatException ex)
            {
                MessageBox.Show("ใส่ค่าคำนวณผิด กรุณาใส่เป็นตัวเลขเท่านั้น");
                txtValue.Focus();
                txtValue.SelectAll();
            }
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {

        }

        private void frmVat_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    {
                        if (panel1.Visible)
                        {
                            btnCancel_Click(null, null);
                        }
                        else
                        {
                            btnClose_Click(null, null);
                        }
                    }
                    break;
                case Keys.F2:
                    {
                        if (panel1.Visible)
                        {
                            btnOK_Click(null, null);
                        }
                        else
                        {
                            btnAdd_Click(null, null);
                        }
                    }
                    break;

                case Keys.F3:
                    {
                        //if (panel1.Visible)
                        //{
                        //    btnOK_Click(null, null);
                        //}
                        //else
                        //{
                        //    btnEdit_Click(null, null);
                        //}
                        btnEdit_Click(null, null);

                    }
                    break;

                case Keys.F4:
                    {
                        btnDelete_Click(null, null);
                    }
                    break;
            }
        }

        #endregion

        #region Function

        private void EnableOrDisableMainPage(bool isEnable)
        {
            btnAdd.Enabled = isEnable;
            btnEdit.Enabled = isEnable;
            btnDelete.Enabled = isEnable;
            btnClose.Enabled = isEnable;
            dataGridView1.Enabled = isEnable;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
        
        private void ClearFields()
        {
            txtId.Text = "";
            txtName.Text = "";
            txtValue.Text = "";
        }

        #endregion

        #region Interface

        void MainFormListener.onMainFormClose()
        {
            // TODO
        }

        public void onTabPageAdded()
        {
            // TODO
        }

        #endregion
        
    }
}
